/* This file has been prepared for Doxygen automatic documentation generation.*/
/*
 *File containing the clock configuraiton and stuff.
 */
#include "clock.h"
#include "avr_compiler.h"
#include <avr/io.h>

void clkInit(void)
{
    /* Set the External Oscillator for an external clock source */
    OSC.XOSCCTRL &= !OSC_XOSCSEL_EXTCLK_gc;
    /* Wait for the external oscillator to become ready */
    while (!(OSC.STATUS & OSC_XOSCRDY_bm)) {
    };
    /* Configure the PLL to select the external clock and set a multiplier of 8 */
    OSC.PLLCTRL = (OSC_PLLSRC_XOSC_gc|(uint8_t)0x08);
    /* Enable the PLL */
    OSC.CTRL |= OSC_PLLEN_bm;
    /* Set the prescalers.  CLKper4 = 128MHz, CLKper2 = 64MHz, CLKper = 32MHz */
    CLK.PSCTRL = (CLK_PSADIV_1_gc|CLK_PSBCDIV_2_2_gc);
    /* Wait for the PLL to stabilize */
    while (!(OSC.STATUS & OSC_PLLRDY_bm)) { };
    /* Set the clock source to the PLL */
    CLK.CTRL = CLK_SCLKSEL_PLL_gc;
}
